package com.zhuanzhuan.hero.user.recent.assembler;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.zhuanzhuan.hero.user.enums.RecentDataEnum;
import com.zhuanzhuan.hero.user.helper.ImHelper;
import com.zhuanzhuan.hero.user.recent.assembler.base.RecentMsgAssembleContext;
import com.zhuanzhuan.hero.user.recent.assembler.base.UserRecentMsgAssembler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;

import static cn.hutool.poi.excel.sax.ElementName.v;

/**
 * @author jiazebiao
 * @date 2024/7/11
 * @description 最近沟通数
 */
@Component
public class CommunicateMsgAssembler implements UserRecentMsgAssembler {
    private static final String COMMUNICATE_MSG = "近%s天和%s名同行交流";
    private static final List<Integer> RECENT_DAYS = Lists.newArrayList(3, 5, 7);
    private static final int IM_MSG_LIMIT = 3;
    @Resource
    private ImHelper helper;
    @Override
    public Map<Long, EnumMap<RecentDataEnum, String>> assemble(RecentMsgAssembleContext context) {
        Map<Long, EnumMap<RecentDataEnum, String>> rst = Maps.newHashMap();
        for(int day : RECENT_DAYS){
            Map<Long, Long> userRecentChatNumMap = helper.batchGetRecentChatNum(context.getUids(), day);
            userRecentChatNumMap.forEach((uid, recentChatNum) -> {
                if(rst.containsKey(uid)){
                    return;
                }
                EnumMap<RecentDataEnum, String>  msgMap = new EnumMap<>(RecentDataEnum.class);
                if(recentChatNum != null && recentChatNum >= IM_MSG_LIMIT){
                    msgMap.put(RecentDataEnum.COMMUNICATE_COUNT, String.format(COMMUNICATE_MSG, day, recentChatNum));
                    rst.put(uid, msgMap);
                }

            });
        }
        return rst;
    }
}